package com.hbsx.why.ui;

import android.content.Context;
import android.graphics.Color;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bigkoo.convenientbanner.ConvenientBanner;
import com.bigkoo.convenientbanner.holder.CBViewHolderCreator;
import com.bigkoo.convenientbanner.holder.Holder;
import com.bigkoo.convenientbanner.listener.OnItemClickListener;
import com.bumptech.glide.Glide;
import com.hbsx.why.R;
import com.hbsx.why.base.BaseActivity;
import com.hbsx.why.utils.LogUtils;
import com.hbsx.why.utils.refresh.RefreshUtil;
import com.hbsx.why.utils.refresh.PullLinearLayout;
import com.hbsx.why.utils.refresh.PullScrollView;
import com.hbsx.why.utils.refresh.PullToRefreshLayout;
import com.hbsx.why.utils.refresh.listener.ScrollViewListener;
import com.hbsx.why.views.BackButton;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by liub on 2017/6/27 18:30.
 */

public class DemoRefreshScrollView extends BaseActivity {
    @BindView(R.id.demo_scrollview)
    PullScrollView demoScrollview;
    @BindView(R.id.pullToRefreshLayout)
    PullToRefreshLayout mPullToRefreshLayout;
    @BindView(R.id.rootPullToRefreshLayout)
    PullToRefreshLayout rootPullToRefreshLayout;
    @BindView(R.id.root_layout)
    PullLinearLayout rootPullLinearLayout;
    @BindView(R.id.banner_viewpage)
    ConvenientBanner convenientBanner;
    @BindView(R.id.top_toolbar)
    FrameLayout topFrameLayout;
    @BindView(R.id.tv_topTitle)
    TextView tvTopTitle;
    @BindView(R.id.skip_to_top)
    LinearLayout skipToTop;
    @BindView(R.id.btn_back)
    BackButton btnBack;
    @BindView(R.id.text_top_right)
    TextView text_top_right;


    List<Integer> integerList;
    private int imageHeight;

    @Override
    protected int getLayoutResId() {
        return R.layout.demo_scrollview_refresh;
    }

    @Override
    public void initView() {
        LogUtils.logi("-----------initView");
//        pullToRefreshLayout= (PullToRefreshLayout) findViewById(R.id.pullToRefreshLayout);
        integerList = new ArrayList<>();
        integerList.add(R.mipmap.banner1);
        integerList.add(R.mipmap.banner2);
        integerList.add(R.mipmap.banner3);
        integerList.add(R.mipmap.banner4);
        integerList.add(R.mipmap.banner5);
        integerList.add(R.mipmap.banner6);
//        text_top_right.setVisibility(View.VISIBLE);
        demoScrollview.setPullDownEnable(true);
        demoScrollview.setPullUpEnable(false);
        rootPullLinearLayout.setPullUpEnable(false);
        rootPullLinearLayout.setPullDownEnable(true);
        /**
         * 头部轮播图片
         */
        if (convenientBanner != null) {
            //开始自动翻页
            convenientBanner.startTurning(3000);
            convenientBanner.setPageTransformer(new ViewPager.PageTransformer() {
                @Override
                public void transformPage(View page, float position) {
                }
            });
            convenientBanner.setPages(new CBViewHolderCreator<NetworkImageHolderView>() {
                @Override
                public NetworkImageHolderView createHolder() {
                    return new NetworkImageHolderView();
                }
            }, integerList).setPageIndicator(new int[]{R.mipmap.point_no, R.mipmap.point_sel})
                    .setPageIndicatorAlign(ConvenientBanner.PageIndicatorAlign.CENTER_HORIZONTAL);
            convenientBanner.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(int position) {
                    toast_short(position + "---=---" + integerList.get(position));
                }
            });
        }
        /**
         * 刷新加载更多
         */
        mPullToRefreshLayout.setOnRefreshListener(new PullToRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh(final PullToRefreshLayout pullToRefreshLayout) {
                pullToRefreshLayout.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(getApplicationContext(), "onRefresh1200", Toast.LENGTH_LONG).show();
                        mPullToRefreshLayout.refreshFinish(true);
                    }
                }, 1200); // 2秒后刷新
            }

            @Override
            public void onLoadMore(final PullToRefreshLayout pullToRefreshLayout) {
                pullToRefreshLayout.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(getApplicationContext(), "onLoadMore", Toast.LENGTH_LONG).show();
                        mPullToRefreshLayout.loadMoreFinish(true);
                    }
                }, 2000); // 2秒后刷新
            }
        });
        rootPullToRefreshLayout.setOnRefreshListener(new PullToRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh(final PullToRefreshLayout pullToRefreshLayout) {
                pullToRefreshLayout.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(getApplicationContext(), "onRefresh1200", Toast.LENGTH_LONG).show();
                        rootPullToRefreshLayout.refreshFinish(true);
                        mPullToRefreshLayout.setVisibility(View.VISIBLE);
                        rootPullToRefreshLayout.setVisibility(View.GONE);
                        initScroll(true);
                    }
                }, 1200); // 2秒后刷新
            }

            @Override
            public void onLoadMore(final PullToRefreshLayout pullToRefreshLayout) {
                pullToRefreshLayout.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(getApplicationContext(), "onLoadMore", Toast.LENGTH_LONG).show();
                        rootPullToRefreshLayout.loadMoreFinish(true);
                        mPullToRefreshLayout.setVisibility(View.VISIBLE);
                        rootPullToRefreshLayout.setVisibility(View.GONE);
                    }
                }, 2000); // 2秒后刷新
            }
        });
        initScroll(true);

    }
    @Override
    protected void onResume() {
        super.onResume();
        //改变刷新图标文字
        RefreshUtil.changeRefreshLableImg(mPullToRefreshLayout);
       // RefreshUtil.changeRefreshLableImg(mPullToRefreshLayout);
    }

    /**
     * 滑动监听
     */
    private void initScroll(boolean flag) {
        if (flag) {
            demoScrollview.scrollTo(0, 0);
            btnBack.setVisibility(View.GONE);
            tvTopTitle.setTextColor(Color.argb((int) 255, 255, 255, 255));
            topFrameLayout.setBackgroundColor(Color.argb((int) 255, 255, 80, 0));
//            tvTopTitle.setTextColor(Color.argb((int) 0, 255, 255, 255));//AGB由相关工具获得，或者美工提供
//            topFrameLayout.setBackgroundColor(Color.argb((int) 0, 255, 80, 0));//AGB由相关工具获得，或者美工提供
            demoScrollview.setScrollViewListener(new ScrollViewListener() {
                @Override
                public void onScrollChanged(PullScrollView scrollView, int x, int y, int oldx, int oldy) {
                    imageHeight = convenientBanner.getHeight()-50;
                    if (y <= 0) {
                        btnBack.setVisibility(View.GONE);
                        tvTopTitle.setTextColor(Color.argb((int) 0, 255, 255, 255));//AGB由相关工具获得，或者美工提供
                        skipToTop.setVisibility(View.GONE);
                        topFrameLayout.setBackgroundColor(Color.argb((int) 0, 255, 80, 0));//AGB由相关工具获得，或者美工提供
                    } else if (y > 0 && y <= imageHeight) {
                        btnBack.setVisibility(View.VISIBLE);
                        skipToTop.setVisibility(View.GONE);
                        float scale = (float) y / imageHeight;
                        float alpha = (255 * scale);
                        // 只是layout背景透明(仿知乎滑动效果)
                        tvTopTitle.setTextColor(Color.argb((int) alpha, 255, 255, 255));
                        topFrameLayout.setBackgroundColor(Color.argb((int) alpha, 255, 80, 0));
                    } else {
                        btnBack.setVisibility(View.VISIBLE);
                        skipToTop.setVisibility(View.VISIBLE);
                        tvTopTitle.setTextColor(Color.argb((int) 255, 255, 255, 255));
                        topFrameLayout.setBackgroundColor(Color.argb((int) 255, 255, 80, 0));
                    }
                }
            });
        } else {
            btnBack.setVisibility(View.VISIBLE);
            skipToTop.setVisibility(View.GONE);
            tvTopTitle.setTextColor(Color.argb((int) 255, 255, 255, 255));
            topFrameLayout.setBackgroundColor(Color.argb((int) 255, 255, 80, 0));
        }
    }

    @OnClick({R.id.tv_topTitle, R.id.text_top_right, R.id.skip_to_top})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_topTitle:
                mPullToRefreshLayout.setVisibility(View.GONE);
                rootPullToRefreshLayout.setVisibility(View.VISIBLE);
                initScroll(false);
                break;
            case R.id.text_top_right:
                mPullToRefreshLayout.setVisibility(View.GONE);
                rootPullToRefreshLayout.setVisibility(View.VISIBLE);
                initScroll(false);
                break;
            case R.id.skip_to_top:
                demoScrollview.scrollTo(0, 0);
                break;
        }
    }

    /**
     * 加载头部轮播图片
     */
    public class NetworkImageHolderView implements Holder<Integer> {
        private ImageView imageView;

        @Override
        public View createView(Context context) {
            imageView = new ImageView(context);
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            return imageView;
        }

        @Override
        public void UpdateUI(Context context, int position, Integer data) {
            //ImageLoaderUtil.displayImage(R.mipmap.banner,imageView);
            //imageView.setBackgroundResource(data);
            //ImageLoaderUtil.displayImage(data.getImg(),imageView);
            Glide.with(context).load(data).into(imageView);
        }
    }

}
